package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("page")
    public ResponseEntity<PageResult<BrandDTO>> pageQuery(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy", required = false) String sortBy,//required = false 表示非必要参数
            @RequestParam(value = "desc", required = false) Boolean desc,
            @RequestParam(value = "key", required = false) String key) {


        return ResponseEntity.ok(this.brandService.pageQuery(page, rows, sortBy, desc, key));
    }

    @PostMapping
    public ResponseEntity<Void> addBrand(
            BrandDTO brandDTO, //编译工具，会根据属性的getter，setter方法，生成RequestParam,requried=false
            @RequestParam("cids") List<Long> cids) {

        this.brandService.addBrand(brandDTO, cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();//创建成功
    }

    @PutMapping
    public ResponseEntity<Void> updateBrand(
            BrandDTO brandDTO, //编译工具，会根据属性的getter，setter方法，生成RequestParam,requried=false
            @RequestParam("cids") List<Long> cids) {

        this.brandService.updateBrand(brandDTO, cids);
        return ResponseEntity.ok().build();//
    }

    @GetMapping("of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategory(@RequestParam("id") Long cid) {

        return ResponseEntity.ok(this.brandService.queryBrandByCategory(cid));
    }

    /**
     * 根据品牌 的id集合查询品牌集合
     * @param ids
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids")List<Long> ids) {

        return ResponseEntity.ok(this.brandService.queryBrandByIds(ids));
    }


    /**
     * 根据品牌的id查询品牌信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id")Long id){
        return ResponseEntity.ok(this.brandService.queryBrandById(id));
    }

}
